package com.fly.caihong.adapter;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fly.caihong.R;
import com.fly.caihong.activity.BrowserImageActivity;
import com.fly.caihong.model.PhotoInfo;
import com.fly.caihong.model.TopicItem;
import com.fly.caihong.utils.ApUtils;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.view.MultiImageView;

import java.util.ArrayList;
import java.util.List;


public class HomeRecommendAdapter extends BaseQuickAdapter<TopicItem, BaseViewHolder> {

    public HomeRecommendAdapter(List<TopicItem> dataList) {
        super(R.layout.layout_recommend_topic_item, dataList == null ? new ArrayList<TopicItem>() : dataList);
    }

    @Override
    protected void convert(BaseViewHolder helper, TopicItem item) {
        helper.setText(R.id.tv_name, item.getNickName()).setText(R.id.tv_date, item.getDate())
                .setText(R.id.tv_title, item.getTopicTitle()).setText(R.id.tv_reply, "评论" + item.getReplyCount());
        GlideUtils.loadCircleAvatar(ApUtils.getApp(), (ImageView) helper.getView(R.id.iv_profile), item.getPhoto());
        MultiImageView miv = helper.getView(R.id.miv_image);
        final ArrayList<PhotoInfo> photoInfos = new ArrayList<>();
        final String[] urls = item.getPictures().split("@");
        final ArrayList<String> urlList = new ArrayList<>();
        for (String url : urls) {
            PhotoInfo photoInfo = new PhotoInfo();
            photoInfo.picUrl = url;
            photoInfos.add(photoInfo);
            urlList.add(url);
        }
        miv.setList(photoInfos);
        miv.setOnItemClickListener(new MultiImageView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                BrowserImageActivity.launchActivity(ApUtils.getApp(), urlList, position);
            }
        });
    }
}
